package cn.byzk.xframe.web.session.annotations;

import cn.byzk.xframe.web.common.constant.enums.CommonResponseEnum;
import cn.byzk.xframe.web.session.util.SessionUtil;
import cn.byzk.xframe.web.session.TokenMemory;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class LoginTokenAspect {

    @Autowired
    private TokenMemory tokenMemory;

    /**
     * 定义切入点
     * @author xcf
     * @date 2021/5/6 9:20
     * @param LoginToken
     */
    @Pointcut(value = "@annotation(LoginToken)", argNames = "LoginToken")
    public void pointcut(LoginToken LoginToken) {

    }


    /**
     * 访问方法前判断是否登录.
     * @author xcf
     * @date 2021/5/6 10:11
     * @param joinPoint
     */
    @Before(value = "@annotation(cn.byzk.xframe.web.session.annotations.LoginToken)")
    public void before(JoinPoint joinPoint) {

        String token = SessionUtil.getToken();
        boolean b = tokenMemory.auth(token);
        if (!b) {
            CommonResponseEnum.IS_NOT_LOGIN.assertFail();
        }

//        SessionUser sessionUser = tokenMemory.getUser(token);
//        log.debug("用户[{}],权限为[{}]，已登录.", sessionUser.getUserNmae(), sessionUser.getPrivName());
    }

}
